| ImageGear Java PDF > Getting Started > Tutorial: Create Your First Project |
In this tutorial, you will learn how to configure a Java project for a console application. You will also learn how to open a PDF and save as a new file.
|
Copy Code | |
|---|---|
import com.accusoft.imagegearpdf.*;
public class MyFirstIGJavaPDFProject
{
private PDF pdf;
private Document document;
static
{
System.loadLibrary("IgPdf");
}
// Application entry point.
public static void main(String[] args)
{
boolean linearized = false;
String inputPath = "sample.pdf";
String outputPath = "sample_output.pdf";;
MyFirstIGJavaPDFProject app = new MyFirstIGJavaPDFProject();
app.loadAndSave(inputPath, outputPath, linearized);
}
// Load and save the PDF file.
private void loadAndSave(String inputPath, String outputPath, boolean linearized)
{
try
{
this.initializePdf();
this.openPdf(inputPath);
this.savePdf(outputPath, linearized);
}
catch (Throwable ex)
{
System.err.println("Exception: " + ex.toString());
}
finally
{
this.terminatePdf();
}
}
// Initialize the PDF session.
private void initializePdf()
{
this.pdf = new PDF();
this.pdf.initialize();
}
// Open input PDF document.
private void openPdf(String inputPath)
{
this.document = new Document();
this.document.openDocument(inputPath);
}
// Save PDF document to the output path.
private void savePdf(String outputPath, boolean linearized)
{
SaveOptions saveOptions = new SaveOptions();
// Set LINEARIZED attribute as provided by the user.
saveOptions.setLinearized(linearized);
this.document.saveDocument(outputPath, saveOptions);
}
// Close the PDF document and terminate the PDF session.
private void terminatePdf()
{
if (this.document != null)
{
this.document.closeDocument();
this.document = null;
}
if (this.pdf != null)
{
this.pdf.terminate();
this.pdf = null;
}
}
}
| |
The com.accusoft.imagegearpdf namespace:
To enable the com.accusoft.imagegearpdf namespace in your project, specify the following directive:
|
Copy Code | |
|---|---|
import com.accusoft.imagegearpdf.*; | |
To initialize and support processing of PDF files we need:
|
Copy Code | |
|---|---|
// Initialize the PDF session.
private void initializePdf()
{
this.pdf = new PDF();
this.pdf.initialize();
}
| |
|
Copy Code | |
|---|---|
private Document document; … this.document = new Document(); this.document.openDocument(inputPath); | |
|
Copy Code | |
|---|---|
SaveOptions saveOptions = new SaveOptions(); // Set LINEARIZED attribute as provided by the user. saveOptions.setLinearized(linearized); this.document.saveDocument(outputPath, saveOptions); | |
See About Linearized PDF Files for more information.
|
Copy Code | |
|---|---|
javac -classpath $HOME/Accusoft/ImageGearJavaPDF1-64/java/IgPdf.jar MyFirstIGJavaPDFProject.java | |
After running this command, you should see a file named MyFirstIGJavaPDFProject.class in your current directory.
|
Copy Code | |
|---|---|
jar cfe MyFirstIGJavaPDFProject.jar MyFirstIGJavaPDFProject MyFirstIGJavaPDFProject.class | |
After running this command, you should see a file named MyFirstIGJavaPDFProject.jar in your current directory.
|
Copy Code | |
|---|---|
export LD_PRELOAD=$HOME/Accusoft/ImageGearJavaPDF1-64/lib/libIGCORE18.so | |
|
Copy Code | |
|---|---|
java -classpath $HOME/Accusoft/ImageGearJavaPDF1-64/java/IgPdf.jar:. MyFirstIGJavaPDFProject | |
After running your sample, you should see a new PDF file, named sample_output.pdf, in your current directory.